/**
 * 
 */
package com.youth.fund.hk.hostManager.manager.web.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.youth.fund.hk.hostManager.manager.datamodel.ContactInformationModel;
import com.youth.fund.hk.hostManager.manager.service.ContactInformationService;

/**
 * @author Sunk_Sky
 *
 * 2014年12月2日下午10:48:38
 */
@Controller
@RequestMapping(value="/contact")
public class ContactController
{
	
	@Autowired
	@Qualifier("ContactInformationService")
	private ContactInformationService service;
	
	@RequiresPermissions("contact:view")
	@RequestMapping( method = RequestMethod.GET)
	public String viewList(Model model)
	{
		ContactInformationModel condition = new ContactInformationModel();
		
		model.addAttribute("contactList", this.service.query(condition));
		
		return "contact/list";
	}
	@RequiresPermissions("contact:read")
	@RequestMapping(value = "/{id}/read", method = RequestMethod.GET)
	public String readContact(@PathVariable int id,
			Model model, RedirectAttributes redirectAttributes)
	{
		ContactInformationModel condition = new ContactInformationModel();
		
		condition.setId(id);
		
		ContactInformationModel cim = this.service.queryOne(condition);
		
		cim.setReaded(1);
		
		this.service.update(cim);
		
		model.addAttribute("contact_information", cim);

		return "contact/read";
	}
	@RequiresPermissions("contact:delete")
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String deleteContact(@PathVariable int id,
			Model model, RedirectAttributes redirectAttributes)
	{
		ContactInformationModel condition = new ContactInformationModel();
		
		condition.setId(id);
		
		boolean result = this.service.delete(condition);
		
		if(result)
		{
			redirectAttributes.addFlashAttribute("msg", "刪除成功");
		}else
		{
			redirectAttributes.addFlashAttribute("msg", "刪除失敗");
		}
		return "redirect:/contact";
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public boolean contactUs (@RequestParam(required=false,defaultValue="") String cust_name,
			@RequestParam(required=false,defaultValue="") String company_name,
			@RequestParam(required=false,defaultValue="") String mobile,
			@RequestParam(required=false,defaultValue="") String e_mail,
			@RequestParam(required=false,defaultValue="") String content)
	{
		if("".equalsIgnoreCase(cust_name)||"".equalsIgnoreCase(mobile)||"".equalsIgnoreCase(content))
		{
			return false;
		}
		
		ContactInformationModel model = new ContactInformationModel();
		
		model.setCust_name(cust_name);
		
		model.setCompany_name(company_name);
		
		model.setContent(content);
		
		model.setE_mail(e_mail);
		
		model.setMobile(mobile);
		
		this.service.create(model);
		
		return true;
	}
	
}
